<template>
  <div>
    <el-dialog
      :title="!dataForm.activityId ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible"
    >
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item>
          <el-input
            v-model="dataForm.key"
            placeholder="参数名"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="getDataList()">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%"
      >
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50"
        >
        </el-table-column>
        <el-table-column
          prop="goodsId"
          header-align="center"
          align="center"
          label="商品id"
        >
        </el-table-column>
        <el-table-column
          prop="goodsName"
          header-align="center"
          align="center"
          label="商品名称"
        >
        </el-table-column>
        <el-table-column
          prop="goodsTitle"
          header-align="center"
          align="center"
          label="商品标题"
        >
        </el-table-column>
        <el-table-column
          prop="goodsImg"
          header-align="center"
          align="center"
          label="商品图片"
        >
          <template slot-scope="scope">
            <img :src="scope.row.goodsImg" width="150px" />
          </template>
        </el-table-column>
        <el-table-column
          prop="goodsDetail"
          header-align="center"
          align="center"
          label="商品详细介绍"
        >
        </el-table-column>
        <el-table-column
          prop="goodsPrice"
          header-align="center"
          align="center"
          label="商品单价"
        >
        </el-table-column>
        <el-table-column
          prop="goodsStock"
          header-align="center"
          align="center"
          label="商品库存"
        >
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="init2(scope.row)"
              >秒杀配置</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>

      <!-- <span slot="footer" class="dialog-footer">
        <el-button type="info" @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </span> -->
    </el-dialog>

    <el-dialog
      :title="!dataForm1.activityId ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible1"
    >
      <el-form
        :model="dataForm1"
        :rules="dataRule"
        ref="dataForm1"
        @keyup.enter.native="dataForm1Submit()"
        label-width="80px"
      >
        <el-form-item label="商品id" prop="goodsId">
          <el-input v-model="dataForm1.goodsId" placeholder="商品id"></el-input>
        </el-form-item>
        <el-form-item label="秒杀价" prop="activityPrice">
          <el-input
            v-model="dataForm1.activityPrice"
            placeholder="秒杀价"
          ></el-input>
        </el-form-item>
        <el-form-item label="库存数量" prop="stockCount">
          <el-input
            v-model="dataForm1.stockCount"
            placeholder="库存数量"
          ></el-input>
        </el-form-item>
        <el-form-item label="秒杀开始时间" prop="startDate">
          <el-date-picker
            v-model="dataForm1.startDate"
            type="datetime"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="秒杀结束时间" prop="endDate">
          <el-date-picker
            v-model="dataForm1.endDate"
            type="datetime"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataForm1Submit1()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>



<script>
export default {
  data() {
    return {
      dataListLoading: false,
      dataList: [],
      visible: false,
      visible1: false,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataForm: {
        activityId: 0,
        goodsId: "",
        goodsImg: "",
        activityPrice: "",
        stockCount: "",
        startDate: "",
        endDate: "",
      },
      dataForm1: {
        activityId: 0,
        goodsId: "",
        goodsImg: "",
        activityPrice: "",
        stockCount: "",
        startDate: "",
        endDate: "",
      },
      dataRule: {
        goodsId: [
          { required: true, message: "商品id不能为空", trigger: "blur" },
        ],
        activityPrice: [
          { required: true, message: "秒杀价不能为空", trigger: "blur" },
        ],
        stockCount: [
          { required: true, message: "库存数量不能为空", trigger: "blur" },
        ],
        startDate: [
          { required: true, message: "秒杀开始时间不能为空", trigger: "blur" },
        ],
        endDate: [
          { required: true, message: "秒杀结束时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    init() {
      this.visible = true;
      this.getDataList();
    },
    init2(goodsvo) {
      this.visible1 = true;
      console.log(goodsvo);
      this.dataForm1.goodsId = goodsvo.goodsId;
      this.dataForm1.goodsImg = goodsvo.goodsImg;
      this.dataForm1.stockCount = goodsvo.goodsStock;
    },
    init1(id) {
      this.dataForm1.activityId = id || 0;
      this.visible1 = true;
      this.$nextTick(() => {
        if (this.dataForm1.activityId) {
          this.$http({
            url: this.$http.adornUrl(
              `/sms/goodsactivity/info/${this.dataForm1.activityId}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            console.log(data);
            if (data && data.code === 0) {
              this.dataForm1.goodsId = data.activityGoodsVo.goodsId;
              this.dataForm1.goodsImg = data.activityGoodsVo.goodsImg;
              this.dataForm1.activityPrice = data.activityGoodsVo.activityPrice;
              this.dataForm1.stockCount = data.activityGoodsVo.stockCount;
              this.dataForm1.startDate = data.activityGoodsVo.startDate;
              this.dataForm1.endDate = data.activityGoodsVo.endDate;
            }
          });
        }
      });
    },
    dataForm1Submit1() {
      this.visible1 = false;
      this.visible = false;
      this.$refs["dataForm1"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sms/goodsactivity/${
                !this.dataForm1.activityId ? "save" : "update"
              }`
            ),
            method: "post",
            data: this.dataForm1,
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // // 表单提交
    // dataFormSubmit() {
    //   this.visible = false;
    //   console.log("准备新增的数据", this.dataListSelections);
    //   if (this.dataListSelections.length > 0) {
    //     let postData = [];
    //     this.dataListSelections.forEach((item) => {
    //       postData.push({ goodsId: item.goodsId, goodsStock: item.goodsStock });
    //       console.log(postData);
    //     });
    //     this.$http({
    //       url: this.$http.adornUrl("/sms/goodsactivity/save"),
    //       method: "post",
    //       data: this.$http.adornData(postData, false),
    //     }).then(({ data }) => {
    //       if (data.code == 0) {
    //         this.$message({ type: "success", message: "添加成功" });
    //       }
    //     });
    //   }
    // },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/pms/goods/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key,
        }),
      }).then(({ data }) => {
        console.log(data);
        if (data && data.code === 0) {
          this.dataList = data.data;
          this.totalPage = data.data.length;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
  },
};
</script>
